package com.library.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.library.dao.BookTitle;
import com.library.dao.BookTitleDAO;
import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;
import com.library.dao.Reservation;
import com.library.dao.ReservationDAO;
import com.library.dao.User;

/**
 * Servlet implementation class UserCheckReversedBook
 */
@WebServlet("/UserCheckReversedBook")
public class UserCheckReversedBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserCheckReversedBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User instantUser = (User)session.getAttribute("instantUser");
		DAOFactory factory = new MySQLDAOFactory();
		ReservationDAO resDao = factory.getReverationDAO();
		BookTitleDAO tiDao = factory.getBookTitleDAO();
		
		ArrayList<Reservation> list = resDao.findReservedBook(instantUser.getUserId());
		ArrayList<ReservationDisplay> array = new ArrayList<ReservationDisplay>();
		for(Reservation res : list){
			ReservationDisplay temp = new ReservationDisplay();
			temp.setBookavailabledate(res.getBookAvailableDate());
			temp.setDate(res.getDate());
			temp.setStatus(res.getStatus());
			BookTitle two = tiDao.findByKey(res.getBookTitleId());
			temp.setTitle(two.getTitle());
			array.add(temp);
		}
		request.setAttribute("reversedBook", array);
		String address = "user/userreversedbook.jsp";
		RequestDispatcher dispatcher = request
				.getRequestDispatcher(address);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
